package cn.severimpl;
import java.util.ArrayList;
import java.util.List;

import cn.daoimpl.StudentDaoJdbcImpl;
import cn.idao.IStudentDao;
import cn.isever.IStudentService;
import cn.lei.PageBean;
import cn.lei.Student;

public class StudentServiceImpl implements IStudentService{
	//private IStudentDao studentDao = new StudentDaoMemoryImpl();
	private IStudentDao studentDao = new StudentDaoJdbcImpl();

	@Override 
	public boolean insert(Student student) {
		int count = studentDao.insert(student);
		return count == 1 ? true : false;
	}

	@Override
	public List<Student> list() {
		List<Student> list = studentDao.list();
		List<Student> copyList = new ArrayList<>();
		for (Student student : list) {
			//student.setName("Java1807" + student.getName());
			copyList.add(student);
		}
		return copyList;
		//return studentDao.list();
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = studentDao.deleteById(id);
		return count == 1 ? true : false;
	}

	@Override
	public Student findById(Integer id) {
		// TODO Auto-generated method stub
		
		return studentDao.findById(id);
	}

	@Override
	public boolean update(Student student) {
		int count = studentDao.update(student);
		return count == 1 ? true : false;
	}

	@Override
	public List<Student> findByName(String name) {
		return studentDao.findByName(name);
	}

	@Override
	public PageBean getpageBean(int pageNo, int pageSize) {
		PageBean pageBean = new PageBean();
		//当前是第几页
		pageBean.setPageNo(pageNo);
		//一页有多少条记录
		pageBean.setPageSize(pageSize);
		//一共有多少条记录
		int totalCount = studentDao.getTotalCount();
		pageBean.setTotalCount(totalCount);
		//一共有多少页
		int totalPage = (int) Math.ceil((double)totalCount/pageSize);
		pageBean.setTotalPage(totalPage);
		//当前页的数据
		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentDao.pageList(offset,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public boolean deleteAll(String[] selectIds) {
		//有缺点：每次打开连接，关闭连接，非常消耗性能
		/*for (String id : selectIds) {
			studentDao.deleteById(Integer.parseInt(id));
		}*/
		
		//批量删除
		int count = studentDao.deleteAll(selectIds);
		return count == selectIds.length ? true : false;

	}

}